﻿using PivalSoft.LDMS.Data;
using PivalSoft.LDMS.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PivalSoft.LDMS
{
    public partial class frmContact : Form
    {
        public int UserId = 0;

        public int contactId = 0;

        public frmContact()
        {
            InitializeComponent();

        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            if (ValidateContact())
            {
                LDMSRepository repository = new LDMSRepository();
                Contact contact = new Contact();
                contact.FirstName = txtFirstName.Text.Trim();
                contact.LastName = txtLastName.Text.Trim();
                contact.ContactTypeId = Convert.ToInt32(cmbContactType.SelectedValue);
                contact.PrimaryContact = txtPrimaryContact.Text.Trim();
                contact.SecondaryContact = txtSecondaryContact.Text.Trim();
                contact.Address1 = txtAddress1.Text.Trim();
                contact.Address2 = txtAddress2.Text.Trim();
                contact.StateID = Convert.ToInt32(cmbState.SelectedValue);
                contact.City = txtCity.Text.Trim();
                contact.ZipCode = txtZipCode.Text.Trim();
                contact.Email = txtEmail.Text.Trim();
                contact.OccuptionID = Convert.ToInt32(cmbOccuption.SelectedValue);
                contact.Tag = txtTag.Text.Trim();
                contact.Notes = txtNotes.Text.Trim();
                if (contactId > 0)
                    contact.ContactID = contactId;
                else
                {
                    contact.CreatedDate = DateTime.Now;
                    contact.CreatedBy = this.UserId;
                }
                repository.AddContact(contact);
                MessageBox.Show("Contact is saved successfully", Resources.MessageCaption);
                ResetControl();
            }
        }

        private void ResetControl()
        {
            contactId = 0;
            txtFirstName.Text = string.Empty;
            txtLastName.Text = string.Empty;
            cmbContactType.SelectedIndex = 0;
            txtPrimaryContact.Text = string.Empty;
            txtSecondaryContact.Text = string.Empty;
            txtAddress1.Text = string.Empty;
            txtAddress2.Text = string.Empty;
            cmbState.SelectedIndex = 0;
            txtCity.Text = string.Empty;
            txtZipCode.Text = string.Empty;
            cmbOccuption.SelectedIndex = 0;
            txtTag.Text = string.Empty;
            txtNotes.Text = string.Empty;
            txtEmail.Text = string.Empty;
        }

        private bool ValidateContact()
        {
            bool isValid = true;
            if (string.IsNullOrEmpty(txtFirstName.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireFirstName, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtLastName.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireLastName, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtPrimaryContact.Text.Trim()))
            {
                MessageBox.Show(Resources.RequirePrimary, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtAddress1.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireAddress, Resources.MessageCaption);
                isValid = false;
            }
            if (string.IsNullOrEmpty(txtCity.Text.Trim()))
            {
                MessageBox.Show(Resources.RequireCity, Resources.MessageCaption);
                isValid = false;
            }
            if (cmbContactType.SelectedIndex == 0)
            {
                MessageBox.Show(Resources.RequireContactType, Resources.MessageCaption);
                isValid = false;
            }
            return isValid;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SearchContact();
        }

        private void SearchContact()
        {
            List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
            if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.NAME, txtName.Text));
            }
            if (!string.IsNullOrEmpty(txtContact.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.CONTACT, txtContact.Text));
            }
            if (!string.IsNullOrEmpty(txtAddress.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.ADDRESS, txtAddress.Text));
            }
            if (!string.IsNullOrEmpty(txtSearchCity.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.CITY, txtSearchCity.Text));
            }
            if (!string.IsNullOrEmpty(txtSearchEmail.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.EMAIL, txtSearchEmail.Text));
            }
            if (!string.IsNullOrEmpty(txtSearchZipCode.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.ZIP, txtSearchZipCode.Text));
            }
            if (!string.IsNullOrEmpty(txtSPrimaryContact.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.PRIMARY, txtSPrimaryContact.Text));
            }
            if (!string.IsNullOrEmpty(txtSearchSecondary.Text.Trim()))
            {
                parameters.Add(new KeyValuePair<string, string>(Constants.SECONDARY, txtSearchSecondary.Text));
            }

            if (parameters.Count > 0)
            {
                LDMSRepository repository = new LDMSRepository();
                var contacts = repository.SearchContact(parameters);
                dgContact.DataSource = contacts;
            }
            else
            {
                MessageBox.Show(Resources.NoneFilter, Resources.MessageCaption);
            }
        }

        private void frmContact_Load(object sender, EventArgs e)
        {
            dgContact.AutoGenerateColumns = false;
            List<TypeMaster> types;

            LDMSRepository repository = new LDMSRepository();
            var typeMasters = repository.GetTypeMasters(new List<string> { "contact", "occuption", "state" });

            types = typeMasters.Where(s => s.Code == "contact").ToList();
            types.Insert(0, new TypeMaster { TypeMasterID = 0, TypeName = "Select Contact" });

            cmbContactType.DataSource = types;
            cmbContactType.ValueMember = "TypeMasterID";
            cmbContactType.DisplayMember = "TypeName";

            types = typeMasters.Where(s => s.Code == "occuption").ToList();
            types.Insert(0, new TypeMaster { TypeMasterID = 0, TypeName = "Select Occupation" });
            cmbOccuption.DataSource = types;
            cmbOccuption.ValueMember = "TypeMasterID";
            cmbOccuption.DisplayMember = "TypeName";

            types = typeMasters.Where(s => s.Code == "state").ToList();
            types.Insert(0, new TypeMaster { TypeMasterID = 0, TypeName = "Select State" });
            cmbState.DataSource = types;
            cmbState.ValueMember = "TypeMasterID";
            cmbState.DisplayMember = "TypeName";

        }

        private void dgContact_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (Convert.ToString(dgContact.Rows[e.RowIndex].Cells[e.ColumnIndex].Value).Equals("Edit"))
            {
                txtFirstName.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["FirstName"].Value);
                txtLastName.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["LastName"].Value);
                txtPrimaryContact.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["PrimaryContact"].Value);
                txtSecondaryContact.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["SecondaryContact"].Value);
                txtAddress1.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["Address1"].Value);
                txtAddress2.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["Address2"].Value);
                txtCity.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["City"].Value);
                txtZipCode.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["ZipCode"].Value);
                txtEmail.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["Email"].Value);

                cmbContactType.SelectedIndex = cmbContactType.Items.IndexOf(cmbContactType.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgContact.Rows[e.RowIndex].Cells["ContactTypeID"].Value)));

                cmbState.SelectedIndex = cmbState.Items.IndexOf(cmbState.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgContact.Rows[e.RowIndex].Cells["StateID"].Value)));

                cmbOccuption.SelectedIndex = cmbOccuption.Items.IndexOf(cmbOccuption.Items.Cast<TypeMaster>().FirstOrDefault(s =>
                    s.TypeMasterID == Convert.ToInt32(dgContact.Rows[e.RowIndex].Cells["OccuptionID"].Value)));

                txtTag.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["Tag"].Value);
                txtNotes.Text = Convert.ToString(dgContact.Rows[e.RowIndex].Cells["Notes"].Value);

                contactId = Convert.ToInt32(dgContact.Rows[e.RowIndex].Cells["ColContactID"].Value);

                dgContact.DataSource = null;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}
